$(function () {
    $("#jqGrid").Grid({
        url: '../sportssmslog/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '手机号', name: 'phone', index: 'phone', width: 40},
			{label: '短信内容', name: 'smsText', index: 'sms_text', width: 240},
			{label: '发送状态', name: 'smsStatus', index: 'sms_status', width: 40,
                formatter: function (value) {
                    if(value === 1)
                        return '<span class="label label-success">发送成功</span>';
                    else if(value === 0)
                        return '<span class="label label-danger">发送失败</span>';
                }
             },
			{label: '发送时间', name: 'send_time', index: 'send_time', width: 80,formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
             }]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		sportsSmsLog: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.sportsSmsLog = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.sportsSmsLog.id == null ? "../sportssmslog/save" : "../sportssmslog/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.sportsSmsLog),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../sportssmslog/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../sportssmslog/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.sportsSmsLog = r.sportsSmsLog;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});